# -*- coding: UTF-8 -*-
# @Time : 2022/1/13 0013 11:32
# @Author : lyw
# @Remark :
import logging

from mxsoftpy.exception import DBError

from db.common.CRM.server_room import IOTRelevanceDB
from utils.classes.custom_exception import VerifyError
from index import _


class IOTAllDeviceDB(IOTRelevanceDB):

    def get_device(self, name):
        """

        @return:
        """
        try:

            total, device_keys = self.tree.open("MXSE", "1\\1.SD", file="ccubase").filter(mxdevicetype__like="*IOT*",
                                                                                          isdel=0,
                                                                                          mxlabel__like="*%s*" % name)
            res_list = []
            prop_list = ["mxlabel", "mxdevicetype"]
            for device_key in device_keys:
                device_value = self.tree.open("MXSE", "1\\1.SD\\%s" % device_key, file="ccubase").items(prop_list)
                device_value["code"] = device_key
                res_list.append(device_value)
            return res_list
        except DBError as e:
            logging.error(e)
            raise VerifyError(_("查询失败"))

    def get_janitor(self, name):
        """
        获取所有的门禁里列表
        @return:
        """
        try:

            total, janitor_keys = self.tree.open("Janitor", "door", file="IOT").filter(is_del=0,
                                                                                       label__like="*%s*" % name)
            res_list = []
            prop_list = ["label"]
            for janitor_key in janitor_keys:
                janitor_value = self.tree.open("Janitor", "door\\%s" % janitor_key, file="IOT").items(prop_list)
                janitor_value["code"] = janitor_key
                janitor_value["mxdevicetype"] = "janitor"
                janitor_value["mxlabel"] = janitor_value.pop("label", "")
                res_list.append(janitor_value)
            return res_list
        except DBError as e:
            logging.error(e)
            raise VerifyError(_("查询失败"))

    def param_middle(self, data_obj):
        """
        查询参数
        @param data_obj:
        @return:
        """
        param = {"iot_code": data_obj.iot_code, "device_type": data_obj.device_type}
        if data_obj.sroom_code != "*":
            param["sroom_code"] = data_obj.sroom_code
        return param
